/*
 * Copyright (C) 2009 Leandro de Oliveira Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.japa.example.blogger;

import java.util.*;

import javax.persistence.*;

@NamedQueries({
	@NamedQuery(name = User.USER_BY_EMAIL, query = "SELECT u FROM User u WHERE u.email = :email"),
	@NamedQuery(name = User.USER_BY_EMAIL_AND_PASSWORD, query = "SELECT u FROM User u WHERE u.email = :email AND u.password = :password")
})
@Entity
public class User {

	public static final String USER_BY_EMAIL = "User.userByEmail";
	public static final String USER_BY_EMAIL_AND_PASSWORD = "User.userByEmailAndPassword";
	
	private String email;

	private String password;
	
	private String name;
	
	private List<Blog> blogs;

	public User() {
		this(null);
	}
	
	public User(String email) {
		this(email, null);
	}

	public User(String email, String password) {
		this.email = email;
		this.password = password;
		
		blogs = new ArrayList<Blog>();
	}

	public void addBlog(Blog blog) {
		blogs.add(blog);
		blog.setOwner(this);
	}
	
// Getters/Setters

	@Id
	public String getEmail() {
		return email;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getPassword() {
		return password;
	}
	
	public void setPassword(String password) {
		this.password = password;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner")
	public List<Blog> getBlogs() {
		return blogs;
	}
	
	public void setBlogs(List<Blog> blogs) {
		this.blogs = blogs;
	}
}
